﻿using System;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES4002
{
    [Plug(ID = "cMES4002",
                   Name = "WIP信息查询",
                   Description = "WIP信息查询",
                   Parent = "0,报表查询")]
    public partial class cMES4002 : StdView
    {      
        public cMES4002()
        {
            InitializeComponent();
        }

        private void InitialLineNameAndGroup()
        {
            ExecutionResult<dynamic> result;
            result = new ExecutionResult<dynamic>();
            //query group name list
            result = Services.ServiceMethod.GetGroupNameList();
            if (result.Status)
            {
                this.combGroup.Items.Clear();
                foreach(var group in result.Data)
                {
                    this.combGroup.Items.Add(group.group_name);
                }
            }
            //query line name list
            result = Services.ServiceMethod.GetLineNameList();
            if (result.Status)
            {
                this.combLineName.Items.Clear();
                foreach (var line in result.Data)
                {
                    this.combLineName.Items.Add(line);
                }
            }
        }

        private void ClearFormInput()
        {
            this.txtMO.Text = "";
            this.txtModel.Text = "";
            this.txtSKU.Text = "";
            this.txtSN.Text = "";
            this.InitialLineNameAndGroup();
            this.dtpStartTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            this.dtpEndTime.Text = DateTime.Now.AddHours(8).ToString("yyyy/MM/dd HH:mm:ss"); //DateTime.Now.AddHours(8).ToString("yyyy/MM/dd HH:mm:ss")
        }

        private bool CheckInputCondition()
        {
            bool bResult = true;

            if(string.IsNullOrEmpty(this.txtSN.Text.Trim()) && 
                string.IsNullOrEmpty(this.txtMO.Text.Trim()) &&
                string.IsNullOrEmpty(this.txtModel.Text.Trim()) && 
                string.IsNullOrEmpty(this.txtSKU.Text.Trim()))
            {
                this.ShowMessage("序号、工单、机种、机型必须有一项输入", MsgType.NG);
                bResult = false;
            }
            if(string.IsNullOrEmpty(this.dtpStartTime.Text.Trim())|| string.IsNullOrEmpty(this.dtpStartTime.Text.Trim()))
            {
                this.ShowMessage("开始时间、结束时间必须同时输入。", MsgType.NG);
                bResult = false;
            }


            return bResult;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (!CheckInputCondition())
            {
                return;
            }
            dynamic qCondition = 
                new
                {
                    sn = this.txtSN.Text.Trim().ToUpper(),
                    mo = this.txtMO.Text.Trim().ToUpper(),
                    model = this.txtModel.Text.Trim().ToUpper(),
                    line = this.combLineName.Text.Trim(),
                    group = this.combGroup.Text.Trim(),
                    sku = this.txtSKU.Text.Trim().ToUpper(),
                    start_time = this.timeChecker.Checked ? this.dtpStartTime.Text : "",
                    end_time = this.timeChecker.Checked ? this.dtpEndTime.Text : ""
                };
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();
            result = Services.ServiceMethod.GetWIPTrackingInfo(qCondition);
            if (!result.Status)
            {
                this.ShowMessage(result.Message, MsgType.NG);                
                return;
            }
            else
            {
                this.ShowMessage(result.Message, MsgType.OK);
            }
            this.datalist.DataSource = result.Data;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if(this.datalist.Rows.Count > 0)
            {
                this.ExportDataToExcel(this.datalist);
                this.ShowMessage("导出Excel成功", MsgType.OK);
            }
        }

        private void cMES4002_Load(object sender, EventArgs e)
        {
            //clear input and initail group and line 
            this.ClearFormInput();
        }

        private void timeChecker_CheckedChanged(object sender, EventArgs e)
        {
            this.dtpStartTime.Enabled = false;
            this.dtpEndTime.Enabled = false;
            if (this.timeChecker.Checked)
            {
                this.dtpStartTime.Enabled = true;
                this.dtpEndTime.Enabled = true;
            }
        }
    }
}
